package cn.iocoder.yudao.module.robot.client;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.robot.client.dto.RobotClueRespDTO;
import cn.iocoder.yudao.module.robot.client.dto.RobotTaskRespDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class RobotClientImpl implements RobotClient {

    private String domain;

    private RobotClueRespDTO culeBeanToResp(JSONObject jsonObject) {
        // 相同字段或json驼峰直接toBean
        return BeanUtils.toBean(jsonObject, RobotClueRespDTO.class);
    }

    @Override
    public List<RobotClueRespDTO> getCuleList(Long tenantId, Long taskId, Long minId, Integer limit) {

        String url = this.domain + "/inner/member/list";
        Map<String, Object> data = new HashMap<>();
        data.put("tenant_id", tenantId);
        data.put("task_id", taskId);
        data.put("min_id", minId);
        data.put("transformed", 2); //转线索状态 1已转入的, 2未转入的 这里固定查询未转入的
        data.put("limit", limit);
        HttpResponse httpResponse = HttpUtil.createPost(url)
                .body(JSONUtil.toJsonStr(data))
                .header(Header.CONTENT_TYPE, ContentType.JSON.getValue())
                .execute();
        String result = httpResponse.body();
        JSONObject jsonObject = JSONUtil.parseObj(result);

        List<RobotClueRespDTO> resultDataList = new ArrayList<>();
        if (ObjectUtil.notEqual(0, jsonObject.getInt("code"))) {
            return Collections.emptyList();
        }
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        if (Objects.nonNull(jsonArray) && !jsonArray.isEmpty()) {
            jsonArray.forEach(jsonData -> {
                resultDataList.add(culeBeanToResp(JSONUtil.parseObj(jsonData)));
            });
        }
        return resultDataList;
    }

    private RobotTaskRespDTO taskBeanToResp(JSONObject jsonObject) {
        // 相同字段或json驼峰直接toBean
        RobotTaskRespDTO resp = BeanUtils.toBean(jsonObject, RobotTaskRespDTO.class);
        return resp;
    }

    @Override
    public List<RobotTaskRespDTO> getTaskList(Long tenantId) {
        String url = this.domain + "/inner/task/taskList";
        Map<String, Object> data = new HashMap<>();
        data.put("tenant_id", tenantId);
        HttpResponse httpResponse = HttpUtil.createPost(url)
                .body(JSONUtil.toJsonStr(data))
                .header(Header.CONTENT_TYPE, ContentType.JSON.getValue())
                .execute();
        String result = httpResponse.body();
        JSONObject jsonObject = JSONUtil.parseObj(result);
        List<RobotTaskRespDTO> resultDataList = new ArrayList<>();
        if (ObjectUtil.notEqual(0, jsonObject.getInt("code"))) {
            return Collections.emptyList();
        }
        JSONArray jsonArray = jsonObject.getJSONArray("data");

        if (Objects.nonNull(jsonArray) && !jsonArray.isEmpty()) {
            jsonArray.forEach(jsonData -> {
                resultDataList.add(taskBeanToResp(JSONUtil.parseObj(jsonData)));
            });
        }
        return resultDataList;
    }
}
